<?php
/**
 * 商品规格
 * User: ws
 * Date: 2018/8/7
 * Time: 16:46
 */

namespace app\admin\controller;
use think\Db;
class Spec extends Init
{
    function _initialize()
    {
        parent::_initialize();
        $this->model = model('goods_type');
        $this->spec = model('spec');
        $this->spec_item = model('spec_item');
    }

    function index(){
        if ($this->request->isAjax()) {
            $info = Db::table('je_spec')
                ->alias('s')
                ->join('je_goods_type g','s.type_id = g.type_id')
                ->field('s.*,g.name as gname')
                ->paginate(input('limit'))->toArray();

            foreach ($info['data'] as $key=>$val){
                $item = Db::name('spec_item')->where("spec_id={$val['spec_id']}")->select();
                $item = array_column($item,'spec_item');
                $info['data'][$key]['item'] = implode(",",$item);
            }

            $total = Db::table('je_spec')
                ->alias('s')
                ->join('je_goods_type g','s.type_id = g.type_id')
                ->field('s.*,g.name as gname')
                ->count();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$info['data'],'count'=>$total]);
        }
        return view('list');
    }



    function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $name = $this->spec->where('name',$data['name'])->find();

            $item = explode("\r\n",$data['spec_item']);

            if(empty($data['spec_id'])){
                if($name){
                    return json(array('code'=>0,'msg'=>'添加失败，不能有相同的名称'));
                }
                unset($data['spec_id']);
                $rul = $this->spec->add($data);
                foreach ($item as $key=>$val){
                    $data1[$key]['spec_id'] = $rul;
                    $data1[$key]['spec_item'] = $val;
                }
                $return = $this->spec_item->saveAll($data1);
            }else{
                $this->spec->isUpdate(true)->allowField(true)->save($data);
                $this->spec->afterSave(input('spec_id'),$data['spec_item']);
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }

        $list = $this->model->get_list('', '', '', 2);
        if(!empty(input('id'))) {
            $where['spec_id'] = input('id');
            $spec = Db::name('spec')->where($where)->find();
            $it = Db::name('spec_item')->where($where)->column('spec_item');
            $spec['items'] = implode(PHP_EOL,$it);
            return view('add',['list'=>$list['data'],'item'=>$spec]);
        }
        $spec['items'] = '';
        $spec['name'] = '';
        $spec['spec_id'] = '';
        $spec['type_id'] = '';
        return view('add',['list'=>$list['data'],'item'=>$spec]);
    }

    function del(){
        $result = $this->spec->destroy(input('spec_id'));
        if($result){
            return json(array('code'=>200,'msg'=>'删除成功'));
        }else{
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
    }
}